package com.czq.controllor;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.czq.common.BaseContext;
import com.czq.common.Result;
import com.czq.pojo.ShoppingCart;
import com.czq.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartControllor {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public Result add(@RequestBody ShoppingCart cart) {
        Long useid = BaseContext.getCurrentId();
        cart.setUserId(useid);
        Long dishId = cart.getDishId();
        Long setmealId = cart.getSetmealId();
        //lam语句
        LambdaQueryWrapper<ShoppingCart> query = new LambdaQueryWrapper();
        query.eq(ShoppingCart::getUserId, useid);
        if(dishId != null){
            //添加的是菜品
            query.eq(ShoppingCart::getDishId,dishId);
        }
        else{
//            添加的是套餐
            query.eq(ShoppingCart::getSetmealId,setmealId);
        }
        //查询要不要加number
        ShoppingCart one = shoppingCartService.getOne(query);
        if (one != null){
            //number需要＋1，
            Integer number = one.getNumber()+1;
            one.setCreateTime(LocalDateTime.now());
            one.setNumber(number);
            shoppingCartService.updateById(one);
        }
        else {
          cart.setNumber(1);
          cart.setCreateTime(LocalDateTime.now());
          shoppingCartService.save(cart);
          one=cart;
        }

        //判断添加的是菜品
//        if (dishId!=null){
//            query.eq(ShoppingCart::getDishId,dishId);
//            ShoppingCart one = shoppingCartService.getOne(query);
//            if (one!=null){
//                Integer number = one.getNumber()+1;
//
//                update.set(ShoppingCart::getNumber,number).eq(ShoppingCart::getDishId,dishId);
//            }
//            else {
//                shoppingCartService.save(cart);
//            }
//
//        }
//        //添加的是套餐
//
//        else {
//
//
//            query.eq(ShoppingCart::getSetmealId,setmealId);
//            ShoppingCart setmeal = shoppingCartService.getOne(query);
//            if (setmeal!=null){
//                Integer number = setmeal.getNumber()+1;
//                update.set(ShoppingCart::getNumber,number).eq(ShoppingCart::getSetmealId,setmealId);
//            }
//            else {
//                shoppingCartService.save(cart);
//            }
//        }
        return  Result.success(one);
    }




//减去套餐

    @PostMapping("/sub")
    public Result sub( @RequestBody ShoppingCart cart){
        Long dishId = cart.getDishId();
        Long useid = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<ShoppingCart>();
        queryWrapper.eq(ShoppingCart::getUserId,useid);
        if (dishId != null){
            //传的是菜品的表
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }
        else {
            //传的是套餐的表
            queryWrapper.eq(ShoppingCart::getSetmealId,cart.getSetmealId());
        }

        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        if (one != null){
            //说明数据库有这个数据，number-1就好了
            Integer number = one.getNumber()-1;
            one.setNumber(number);
            one.setCreateTime(LocalDateTime.now());
            if (number==0){
                shoppingCartService.removeById(one);
            }
            else{
                shoppingCartService.updateById(one);
            }
        }
        else{
            //说明数据库没这个数据，添加数据到数据库
            cart.setNumber(1);
            cart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(cart);
            one=cart;
        }
        return  Result.success(one);



    }


    //查看购物车
    @GetMapping("/list")
    public Result  getlist(){
        Long userid = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> query = new LambdaQueryWrapper();
        query.eq(ShoppingCart::getUserId,userid);
        query.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(query);
        return Result.success(list);

    }
//    清空购物车

    @DeleteMapping("/clean")
    public Result delete(){
        Long userid = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> query = new LambdaQueryWrapper();
        query.eq(ShoppingCart::getUserId,userid);
        shoppingCartService.remove(query);
        return  Result.success("清空成功");

    }
}




